<template>
	<view class="box" :style="theme">
		<u-sticky bgColor="#fff" style="top: 0;">
			<view class="head flex-cb">
				<view class="head-left flex-c">
					<image :src="imgHost+'/ar_left.png'" @click="weekChange"></image>
					<text>{{weekName}}</text>
					<image :src="imgHost+'/ar_right.png'" @click="weekChangeb"></image>
				</view>
				<view class="head-right f28 ztColor" @click="weekChangec">回到本周</view>
			</view>
			<view class="topTab">
				<view class="left">
					<u-tabs :list="tab" lineHeight="6rpx" :scrollable="false"
						:activeStyle="{fontWeight: 'bolder',fontSize: '34rpx',color: '#222222'}"
						:inactiveStyle="{fontSize: '30rpx'}" @click="tabclick" :current="swiperIndex">
					</u-tabs>
				</view>
			</view>
		</u-sticky>
		<view v-if="swiperIndex != 2" class="content">
			<view class="xxkc" v-for="(item, index) in list" :key="index">
				<view class="tabHead flex flex_between" :class="item.signInStatus==0?'zs':item.signInStatus==5?'on':item.signInStatus==1 || item.signInStatus==2 || item.signInStatus==4?'hui':''" @click="jumpDetail(item)">
					<view class="left flex"><u-icon name="clock" :color="item.signInStatus==0?'#fff':item.signInStatus==5?'#333333':'#666'"size="28rpx"></u-icon><text>{{item.day.slice(5, 10)+ ' ' + item.days + ' ' + item.time}}</text></view>
					<view class="right">
						{{item.signInStatus==0?'未上课':item.signInStatus==1?'已签到':item.signInStatus==2?'缺课':item.signInStatus==4?'已补课':'待成班'}}
					</view>
				</view>
				<view class="flex flex_between cont" @click="jumpDetail(item)">
					<image class="left" :src="item.courseImage"></image>
					<view class="right">
						<view class="name">{{item.venueName ? item.venueName + ' | ':''}}{{item.courseName}}</view>
						<view v-if="item.site" class="dd">地点：{{item.site}}</view>
						<view v-if="item.teacher"  class="dd">教练：{{item.teacher}}{{item.teacherLevelName?' ｜ ' + item.teacherLevelName : ''}}</view>
						<view v-if="item.assistantName" class="dd">助教：{{item.assistantName}}</view>
						<view v-if="item.studentName" class="dd">报名人：{{item.studentName}}</view>
					</view>
				</view>
				<!-- <view v-if="item.signInStatus==0" class="yicon" @click="jumpDetail(item)">
					<image :src="imgHost+'/ic_erweima.png'"></image>
				</view> -->
			</view>
			<u-loadmore v-if="end && list.length >= 10" :status="status" line />
		</view>
		<view v-if="list.length === 0" class="no-order">
			<image :src="imgHost+'/default_nodata.png'"></image>
			<view>暂无数据</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				list: [{}],
				presentTime: new Date(new Date().toLocaleDateString()).getTime(),
				page: 1,
				noClick: true,
				end: false,
				status: 'nomore',
				firstQueryTime: '',
				tab: [{
					name: "待开课",
					value: '0',
					triggered: false,
				}, {
					name: "已开课",
					value: '1',
					triggered: false,
				}],
				swiperIndex: 0,
				currentDate: '',
				weekList: [],
				weekName: '',
				monthDay: new Date()
			}
		},
		onLoad() {
			this.firstQueryTime = this.$app.firstQueryTime()
			this.setDate(new Date())
		},
		methods: {
			jump(url, params, a) {
				this.$app.jump(url, params, a);
			},
			jump2(url, params, a) {
				this.$app.jump2(url, params, a);
			},
			//单张图片预览
			previewImg(imgurl) {
				let _this = this;
				let imgsArray = [];
				imgsArray[0] = imgurl
				uni.previewImage({
					current: 0,
					urls: imgsArray
				});
			},
			tzDetail(item) {
				this.jump('/pages/curriculum/detailb?id=', item.courseId)
			},
			addDate(date, n) {
				date.setDate(date.getDate() + n)
				return date
			},
			// 获取星期
			calculation(s) {
				return s < 10 ? '0' + s : s
			},
			formatDate(date) {
				const year = date.getFullYear() + '-'
				const month = this.calculation(date.getMonth() + 1)
				const day = this.calculation(date.getDate())
				const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()]
				return {
					label: month + '/' + day,
					value: year + month + '-' + day,
					old: new Date(date)
				}
			},
			setDate(date) {
				var week = date.getDay() - 1
				if (week === -1) {
					week = 6
				}
				date = this.addDate(date, week * -1)
				this.currentDate = new Date(date)
				console.log(33, date, new Date(date))
				const arr = []
				for (var i = 0; i < 7; i++) {
					arr[i] = this.formatDate(i === 0 ? date : this.addDate(date, 1))
				}
				this.weekList = arr
				this.weekName = arr[0].label + '~' + arr[6].label
				this.list = [];
				this.page = 1;
				this.end = false;
				this.getList()
			},
			weekChange() {
				// if (this.weekList[0].old.getTime() < this.presentTime) return uni.showToast({
				// 	title: '不能再往前了',
				// 	icon: 'none',
				// 	duration: 2000
				// })
				this.setDate(this.addDate(this.currentDate, -7))
			},
			weekChangeb() {
				this.setDate(this.addDate(this.currentDate, 7))
			},
			weekChangec() {
				this.setDate(new Date())
			},
			previewImage(e) {
				let info = JSON.stringify(e)
				this.jump('/pages/member/cardDetails?info=', info)
			},
			jumpDetail(e) {
				// if (e.signInStatus == 0) {
				// 	let obj = {
				// 		...e
				// 	}
				// 	obj.studentList = null
				// 	let info = JSON.stringify(obj)
				// 	this.jump2('/pages/member/myCardDetail?info=', info)
				// } else {
				// 	this.jump2('/pages/curriculum/detail?id=', e.courseId)
				// }
				this.jump2('/pages/member/myCardDetails?id=', e.id + '&timeId=' + e.timeId)
			},

			tabclick(e) {
				this.swiperIndex = e.index;
				this.list = [];
				this.page = 1;
				this.end = false;
				this.getList()
			},
			getList() {
				this.$app.ajax({
					api: this.$api.myCourseList(),
					data: {
						pagination: {
							page: this.page,
							firstQueryTime: this.firstQueryTime,
							rows: 10
						},
						param: {
							memberId: this.userInfo.id,
							begin: this.swiperIndex != 2 ? this.weekList[0].value : '',
							end: this.swiperIndex != 2 ? this.weekList[6].value : '',
							flag: '2',
							openStatus: this.tab[this.swiperIndex].value,
							venueId: this.venueId
						}
					}
				}).then((res) => {
					if (res.errorCode === '000000') {
						if (!res.recordList || res.recordList.length < 10) {
							this.end = true
						}
						if (res.recordList) {
							let arr = res.recordList
							// if (this.swiperIndex == 0) {
							// 	arr.forEach(e => {
							// 		let objs = e.studentList || []
							// 		let idsStr = objs.map(function(obj, index) {
							// 			return obj.name;
							// 		}).join(",");
							// 		e.student = idsStr
							// 	})
							// }
							this.list = this.list.concat(arr)
						}
					}
				}).catch(() => {})
			},
			reachBottom() {
				if (!this.end) {
					this.page += 1;
					this.getList();
				}
			},
			// 触底加载更多数据
			onReachBottom(e) {
				this.reachBottom()
			}

		}

	}
</script>
<style scoped lang="scss">
	.topTab {
		box-sizing: border-box;
		position: relative;
		// border-bottom: 1rpx solid #E5E5E5;
		width: 690rpx;
		margin: 0 auto;

		&::after {
			content: '';
			position: absolute;
			left: 0rpx;
			right: 0rpx;
			top: 1rpx;
			z-index: 0;
			height: 1px;
			background-color: #E5E5E5;
		}

		.left {
			width: 320rpx;
			z-index: 99;
			margin-left: -20rpx;
		}
	}


	.content {
		padding: 30rpx;
		box-sizing: border-box;
	}

	.head {
		box-sizing: border-box;
		line-height: 50rpx;
		padding: 30rpx;
	}

	.head-left image {
		width: 30rpx;
		height: 30rpx;
	}

	.head-left text {
		font-size: 32rpx;
		line-height: 50rpx;
		font-weight: bolder;
		color: #111111;
		display: inline;
		margin: 0 20rpx;
	}

	.head-right {
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
	}

	.xxkc {
		width: 100%;
		background: #FFFFFF;
		border-radius: 8rpx;
		overflow: hidden;
		position: relative;
		box-sizing: border-box;
		margin-bottom: 20rpx;

		.tabHead {
			font-size: 28rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #fff;
			background: $color;
			line-height: 40rpx;
			padding: 26rpx 24rpx;
			box-sizing: border-box;
			.left {
				text {
					margin-left: 6rpx;
				}
			}
			&.zs {
				color: #fff;
				background: $color;
			}

			&.on {
				color: #333333;
				background: #F8EFE0;
				.right {
					color: #F95601;
				}
			}
			&.hui {
				color: #666666;
				background: #DDDDDD;
			}

			
		}

		.cont {
			align-items: flex-start;
			padding: 20rpx 24rpx 30rpx;
			box-sizing: border-box;

			.left {
				width: 156rpx;
				height: 88rpx;
				border-radius: 8rpx;
				flex: none;
				margin-right: 20rpx;
			}

			.right {
				flex: 1;

				.name {
					font-size: 30rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #222222;
					line-height: 42rpx;
				}

				.dd {
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #888888;
					margin-top: 8rpx;
				}

				.jl {
					font-size: 26rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					color: #888888;
				}
			}

		}

		.yicon {
			position: absolute;
			box-sizing: border-box;
			padding: 12rpx;
			background-color: $color;
			bottom: 0;
			right: 0;
			border-radius: 8rpx 0 0 0;

			image {
				width: 40rpx;
				height: 40rpx;
				vertical-align: top;
			}
		}

		.xxkc-foot {
			position: absolute;
			box-sizing: border-box;
			padding: 0 10rpx;
			color: #fff;
			line-height: 40rpx;
			font-size: 24rpx;
			background-color: $color;
			bottom: 20rpx;
			right: 30rpx;
			border-radius: 20rpx;
		}

	}
</style>